package com.ishare.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ishare.admin.mapper.SysRoleMapper;
import com.ishare.admin.service.SysRoleService;
import com.ishare.admin.service.SysUserRoleService;
import com.ishare.admin.service.SysRoleMenuService;
import com.ishare.admin.service.SysUserService;
import com.ishare.common.constant.Constant;
import com.ishare.common.entity.po.SysRole;
import com.ishare.common.exception.BusinessException;
import com.ishare.common.page.PageUtils;
import com.ishare.common.utils.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 角色
 * @author Mark sunlightcs@gmail.com
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {
    @Autowired
    private SysRoleMenuService SysRoleMenuService;

    @Autowired
    private SysUserService SysUserService;

    @Autowired
    private SysUserRoleService sysUserRoleService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String roleName = (String) params.get("roleName");
        Long createBy = (Long) params.get("createBy");

        IPage<SysRole> page = this.page(
            new Query<SysRole>().getPage(params),
            new QueryWrapper<SysRole>()
                .like(StringUtils.isNotBlank(roleName), "role_name", roleName)
                .eq(createBy != null, "create_by", createBy)
        );
        for (SysRole record : page.getRecords()) {
            Long roleId = record.getId();
            List<Long> roleIds = SysRoleMenuService.queryMenuIdList(roleId);
            record.setMenuIdList(roleIds);
        }
        return new PageUtils(page);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveRole(SysRole role) {
        role.setCreateTime(new Date());
        this.save(role);

        //检查权限是否越权
        checkPrems(role);

        //保存角色与菜单关系
        SysRoleMenuService.saveOrUpdate(role.getId(), role.getMenuIdList());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(SysRole role) {
        this.updateById(role);

        //检查权限是否越权
        checkPrems(role);

        //更新角色与菜单关系
        SysRoleMenuService.saveOrUpdate(role.getId(), role.getMenuIdList());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] roleIds) {
        //删除角色
        this.removeByIds(Arrays.asList(roleIds));

        //删除角色与菜单关联
        SysRoleMenuService.deleteBatch(roleIds);

        //删除角色与用户关联
        sysUserRoleService.deleteBatch(roleIds);
    }


    @Override
    public List<Long> queryRoleIdList(Long createUserId) {
        return baseMapper.queryRoleIdList(createUserId);
    }

    /**
     * 检查权限是否越权
     */
    private void checkPrems(SysRole role) {
        //如果不是超级管理员，则需要判断角色的权限是否超过自己的权限
        /*if (role.getCreateBy() == Constant.SUPER_ADMIN) {
            return;
        }*/

        //查询用户所拥有的菜单列表
       /* List<Long> menuIdList = SysUserService.queryAllMenuId(role.getCreateUserId());

        //判断是否越权
        if (!menuIdList.containsAll(role.getMenuIdList())) {
            throw new BusinessException("新增角色的权限，已超出你的权限范围");
        }*/
    }
}
